package Version1;


import java.util.Scanner;

import javax.swing.JFrame;
import javax.swing.JOptionPane;


public class Main {


	/**
	 * @param args
	 */
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);

		JFrame frame = new JFrame();
		frame.setResizable(false);
		frame.setSize(400, 422);

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


		//Get name  -- Make GUI prettier?
		String name  = JOptionPane.showInputDialog("Insert your name");
		frame.setTitle(name);
		int selector = JOptionPane.showConfirmDialog(null, "Auto Generated game?", "Selector", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);

		//Get grid size
		if(selector == JOptionPane.YES_OPTION)
		{	
			int size = 0; 
			do {
				try {
					size = Integer.parseInt(JOptionPane.showInputDialog("Insert size.\nSize must be between 3 and 6. (Inclusive)"));	
				} catch (Exception e) {}

			} while (size < 3 || size > 6); //Make sure input is on the right dimension
			Controls controls = new Controls(size);
			frame.addKeyListener(controls);
			frame.add(controls);
			frame.setVisible(true);
		}
		else if (selector == JOptionPane.NO_OPTION) 
		{
			System.out.println("Insert the values starting with the grid size and followed by the appropiate order");
			System.out.print(">>NPG ");
			String[] reader = (input.nextLine().split(", ")); 
			int[] vector = new int[(int) Math.pow(Integer.parseInt(reader[0]), 2)];
			if(Math.sqrt(vector.length) < 3 || Math.sqrt(vector.length) > 6)
			{
				System.out.println("Incorrect value");
				System.exit(0);
			}
			for (int i = 1; i < reader.length; i++) {
				if (reader[i].equals("0")) {
					reader[i] = Integer.toString(vector.length);
				}
				vector[i-1] = Integer.parseInt(reader[i]);

			}
			Controls controls = new Controls(vector);
			frame.addKeyListener(controls);
			frame.add(controls);
			frame.setVisible(true);
		}
		else {
			System.exit(0);
		}

	}


}
